package com.dianpingmedia.common;

import com.dianpingmedia.simulate.configuration.SimulateConfiguration;
import com.dianpingmedia.simulate.model.SimulateUploadEntity;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Map;

@Component
public class NoticeUtil {
    private static final String NOTICE_URL = "impress/";
    @Autowired
    SimulateConfiguration configuration;

    public String createNoticeUrl(Integer dspId
            , String screenId
            , String reqId
            , Integer type
            , Integer returnCode
            , Map<String, String> macros
            , SimulateUploadEntity.Request material) {


        StringBuilder sb = new StringBuilder();
        String outnet = configuration.getOutnet();
        if (!StringUtils.isEmpty(outnet)) {
            sb.append(outnet).append(NOTICE_URL);
        } else {
            sb.append("http://").append(getIpAddress()).append(":").append(port).append(NOTICE_URL);
        }
        if (type == 0) {
            sb.append("begin?");
        } else if (type == 1) {
            sb.append("end?");
        } else {
            sb.append("win?");
        }
        sb.append("issuedTime=")
                .append(System.currentTimeMillis())
                .append("&type=").append(type)
                .append("&adId=").append(material.getId())
                .append("&code=").append(returnCode == null ? 200 : returnCode)
                .append("&screenId=").append(screenId)
                .append("&id=").append(reqId)
                .append("&duration=").append(material.getDuration())
                .append("&dspId=").append(dspId);
        if (macros != null) {
            for (Map.Entry<String, String> entry : macros.entrySet()) {
                sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
        }
        return sb.toString();
    }


    @Value("${server.port}")
    private int port = 8089;

    private static String getIpAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
